import pandas as pd
from .util import post

url = "http://192.168.1.247:18000"


def parse_labels(label_path):
    id2label = {}
    df = pd.read_csv(label_path, header=None, sep='\t')
    for i in range(len(df)):
        id2label[str(i)] = df.iloc[i][0]
    return id2label


def eval_data(data_path, label_path):
    id2label = parse_labels(label_path)
    df = pd.read_csv(data_path, header=None, sep='\t')
    acc_number = 0
    for i in range(len(df)):
        text = df.iloc[i][0]
        actual_label = df.iloc[i][1]
        predicted_label = inference_text(text)
        predicted_label = id2label[predicted_label]
        if actual_label == predicted_label:
            acc_number += 1
            print(f'number: {i}, total: {len(df)}, right: {acc_number}')

    print(f'total: {len(df)}, right: {acc_number}, acc: {acc_number*100/len(df)}')


def inference_text(text):
    path = "/inference/text/single_label"
    params = {"model_name": "bert_chinese", "text": text}
    response = post(url, path, params=params)
    return response.text


if __name__ == '__main__':
    label_path = "data/label.txt"
    data_path = "data/train.txt"
    eval_data(data_path, label_path)